#include "SimDriverBoard.h"
#include <iostream>
#include <atomic>
#include <mutex>
#include <csignal>
#include <condition_variable>

std::mutex g_mutex;
std::atomic_bool g_exit{false};
std::condition_variable g_cv;

void SigHandler(int signal)
{
    std::cout << "receive INT, program exit!" << std::endl;
    std::unique_lock<std::mutex> lock(g_mutex);
    g_exit = true;
    g_cv.notify_all();
    std::cout << "notify all" << std::endl;
}

void WaitExit()
{
    std::unique_lock<std::mutex> lock(g_mutex);
    g_cv.wait(lock, []()
    {
        return g_exit.load();
    });
    std::cout << "wait exit" << std::endl;
}



int main()
{
    std::cout << "=========================== SimDriverBoard start!!! ==========================" << std::endl;
    sigset(SIGINT, SigHandler);

    // 创建/获取模拟底板的对象
    SimDriverBoard& simDriverBoard = SimDriverBoard::Instance();

    // 初始化注册各个服务模块
    if (!simDriverBoard.Init())
    {
        std::cout << "SimDriverBoard::Init() error" << std::endl;
        return -1;
    }

    // 准备好了进行通讯服务
    if (!simDriverBoard.Start())
    {
        std::cout << "SimDriverBoard::Start() error" << std::endl;
        return -1;
    }


    // 等待退出
    WaitExit();

    simDriverBoard.DestroyManagers();
    std::cout << "=========================== SimDriverBoard exit!!! ==========================" << std::endl;
    return 0;
}